import 'package:dmz/base/controller/base_controller.dart';
import 'package:dmz/business/home/controller/home_main_controller.dart';
import 'package:dmz/business/home/model/moto_model.dart';
import 'package:dmz/business/mine/model/share_model.dart';
import 'package:dmz/request/apis.dart';
import 'package:dmz/request/request_client.dart';
import 'package:dmz/routers/routers.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class GarageController extends BaseController {
  late List<MotoModel> motoList;
  RxInt selectIndex = 0.obs;
  int motoSelect = 0;
  PageController pageController = PageController();
  List<ShareModel> acceptModels = [];
  List<ShareOutModel> shareOutModels = [];
  @override
  void loadNet() {}

  @override
  void onHidden() {}

  @override
  void onInit() {
    motoList = Get.find<HomePageController>().motoList;
    motoSelect = Get.find<HomePageController>().pageIndex;
    super.onInit();
  }

  changePage(int index) {
    selectIndex.value = index;
    if (index == 1) {
      getUserShareList();
    }

    if (index == 2 && acceptModels.isEmpty) {
      getAcceptShareList();
    }

    pageController.jumpToPage(index);
  }

  void getUserShareList() {
    RequestClient()
        .post<ShareOutPageData>(APIS.userShareList, data: {}).then((value) {
      if (value != null) {
        shareOutModels = value.list ?? [];
        update(['share']);
      }
    });
  }

  void getAcceptShareList() {
    RequestClient().post<SharePageData>(APIS.shareDeviceList,
        data: {'type': 1}).then((value) {
      if (value != null) {
        acceptModels = value.list ?? [];
        update(['accept']);
      }
    });
  }

  addDevice(ShareModel data) {
    RequestClient()
        .post<Map>(APIS.acceptShare, data: {'id': data.id}).then((value) {
      Get.back();
    });
  }

  void addMoto() {
    Get.toNamed(RouteGet.addBicke);
  }
}
